package cn.tedu.csmall.stock.webapi.quartz.rabbit;

import cn.tedu.csmall.commons.pojo.stock.entity.Stock;
import cn.tedu.csmall.stock.webapi.quartz.config.RabbitMQConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.math.RandomUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

@Slf4j
public class RabbitMQJob implements Job {

    // 装配能够想RabbitMQ发送消息的对象
    // 这个对象也是在添加依赖后,项目启动时,自动保存到Spring容器的
    @Autowired
    private RabbitTemplate rabbitTemplate;

    static int i=1;
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        // 实例化Stock对象
        Stock stock=new Stock();
        stock.setId(i++);
        stock.setCommodityCode("PC100");
        stock.setReduceCount(1+ RandomUtils.nextInt(10));
        // 下面开始发送消息,指定交换机名称和路由key名称
        rabbitTemplate.convertAndSend(
                RabbitMQConfig.STOCK_EX,
                RabbitMQConfig.STOCK_ROUT,
                stock);
        log.info("消息发送完成:{}",stock);
    }
}
